﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.Enums;

namespace BuildPlannerLib.SimEvents
{
    // Derived class from Event for a worker having travelled the distance needed to mine close mineral patches
    public class ReachCloseMinerals : SimEvent
    {
        // Field for the base at which mining is taking place
        public Base AtBase;

        // Constructor with parameters for parent build, base, and time
        public ReachCloseMinerals(Build build, Base b, int time)
        {
            Build = build;
            AtBase = b;
            Time = time;
        }

        // If there is an idle close patch, starts mining from it. Otherwise, sets the worker to the Close category and adds an event for reaching far mineral patches
        public override void Execute()
        {
            AtBase.MineralWorkerStatus[0]--;
            MineralFlags mineralflags;
            int newTime = Time;
            if (AtBase.IdleMineralPatches[3] > 0)
            {
                mineralflags = MineralFlags.GoldPatch & MineralFlags.ClosePatch;
                newTime += 294;
                AtBase.IdleMineralPatches[3]--;
                Build.EventList.Add(new FinishMineralMining(Build, AtBase, mineralflags, newTime));
            }
            else if (AtBase.IdleMineralPatches[2] > 0)
            {
                mineralflags = MineralFlags.ClosePatch;
                newTime += 294;
                AtBase.IdleMineralPatches[2]--;
                Build.EventList.Add(new FinishMineralMining(Build, AtBase, mineralflags, newTime));
            }
            else
            {
                AtBase.MineralWorkerStatus[1]++;
                newTime += 52;
                Build.EventList.Add(new ReachFarMinerals(Build, AtBase, newTime));
            }
        }

        public override Base GetBase()
        {
            return AtBase;
        }
    }
}
